<?php
use services\models\Product;
use services\models\Goods;

/** @var \services\models\Order $order */
$errs = $order->getErrors();
?>

<?php $this->beginBlock('css'); ?>
<link rel="stylesheet" type="text/css" href="<?= Yii::getAlias('@static') ?>/pc/liketry/css/shop.css">
<?php $this->endBlock(); ?>

<?= $this->render('@liketry/views/layouts/header_shop.php', ['step' => 2]) ?>

<div class="main settle-main">
    <div class="settle-title">填写并核对订单信息</div>
    <!--收货人信息-->
    <div class="settle-receiver">
        <h2 class="settle-sub-title">收货人信息</h2>
        <ul class="settle-receiver-address" id="settleAddress"></ul>
        <p class="settle-noaddress" id="settleNoAddress">为了快递能更快更准确的给您派件，需要您重新填写的收货地址，给您造成的不便敬请谅解</p>
        <div class="settle-receiver-do"><span class="settle-address-add">添加新地址<i>+</i></span><span
                class="settle-address-more"><i>更多地址</i><span class="icon icon-arrow-down3"></span></span></div>
    </div>
    <?php if (!$cellConfirm): ?>
        <!--手机验证-->
        <div class="settle-phone-check">
            <h2 class="settle-sub-title">手机验证</h2>
            <div class="settle-phone-check-form">手机号码：<input type="tel" maxlength="11" id="checkCell" class="width156">验证码：<input
                    type="text" class="width90" id="cellCode"><span class="settle-phone-check-code" id="getCellCode">获取短信验证码</span><span
                    class="settle-phone-check-btn dis" id="checkCellCode">确定</span></div>
        </div>
    <?php endif; ?>
    <?php if (0): #todo:暂不做 ?>
        <!--优惠券-->
        <div class="settle-coupon">
            <h2 class="settle-sub-title">优惠券</h2>
            <div class="settle-coupon-tips">如果您有优惠券(现金券、折扣券或礼品券)，可在此输入使用。</div>
            <div class="settle-coupon-use"><span class="icon icon-stock"></span><span>使用优惠券</span></div>
            <div class="settle-coupon-info">
                优惠券信息
            </div>
        </div>
    <?php endif; ?>
    <!--支付方式-->
    <div class="settle-pay-method">
        <h2 class="settle-sub-title">支付方式</h2>
        <ul class="settle-pay-method-list">
            <li class="selected">在线支付<span class="icon icon-pay-selected"></span></li>
            <li class="dis">货到付款<span class="icon icon-pay-selected"></span></li>
        </ul>
        <div class="settle-pay-balance"><span class="settle-pay-balance-use"><span
                    class="icon icon-stock<?= $order->memberInfoBestry->member_money == 0 ? ' dis' : '' ?>"
                    id="useBalanceSelect"></span><span
                    class="settle-pay-balance-amount">使用余额支付（账户剩余<i
                        id="settleBalanceAmount"><?= $order->memberInfoBestry->member_money ?></i>元）</span></span><span
                class="error"></span></div>
        <?php if ($order->memberInfoBestry->member_money != 0): ?>
            <div class="settle-balance-info">
                <div class="settle-balance-use-amount"><span>请输入您想使用的余额：</span><input type="text"
                                                                                      id="settleBalanceUseAmount<?= !$cellConfirm ? 'Uncheck' : '' ?>"
                                                                                      class="width90"><?= !$cellConfirm ? '<span class="settle-phone-check-code" id="settleBalanceUseConfirm">确定</span>' : '' ?>
                    <span class="error"></span></div>
                <?php if ($cellConfirm): ?>
                    <div class="settle-phone-check-form">您的绑定手机号码为：<span
                            id="cellSettleBalance"><?= $order->memberInfoBestry->memberInfo->cell ?></span><input
                            type="text" class="width90" id="cellCodeSettleBalance"><span class="settle-phone-check-code"
                                                                                         id="getCellCodeSettleBalance">获取短信验证码</span><span
                            class="settle-phone-check-btn" id="checkCellCodeSettleBalance">确定</span><span
                            class="error"></span>
                    </div>
                <?php endif; ?>
            </div>
        <?php endif; ?>
    </div>
    <!--商品列表-->
    <div class="settle-goods-list">
        <h2 class="settle-sub-title">商品列表</h2>
        <div class="shop-title">
            <div class="shop-image">商品</div>
            <div class="shop-name">商品名称</div>
            <div class="shop-size">规格</div>
            <div class="shop-price">单价</div>
            <div class="shop-amount">数量</div>
            <div class="shop-sum">金额小计</div>
        </div>
        <?php if (!key_exists('fatal', $errs)): ?>
            <div class="shop-list">
                <ul>
                    <?php if (!empty($fullcut)): ?>
                        <li>
                        <div class="shop-discount"><em>满减</em> <?= $fullcut; ?></div></li><?php endif; ?>
                    <?php foreach ($order->productsInCart as $od):
                        $goodPic = Goods::findByGoodsIdFromRedis($od->goods_id, ['pic']);
                        ?>
                        <li>
                            <div class="shop-image"><a target="_blank"
                                                       href="<?= Yii::$app->urlManager->createUrl(["goodsdetail/index", 'product_id' => $od->product->product_id]); ?>"><img
                                        src="<?= Yii::$app->params['goods_image_url'] . str_replace('./upload', 'upload', $goodPic['pic']) ?>"></a>
                            </div>
                            <div class="shop-name">
                                <p><a target="_blank"
                                      href="<?= Yii::$app->urlManager->createUrl(["goodsdetail/index", 'product_id' => $od->product->product_id]); ?>"><?= $od->goodsDetail->goods_name ?></a>
                                </p>
                                <div class="shop-tags">
                                    <?php if ($od->product->type == Product::$_TYPE_TRY): ?>
                                        <span class="icon icon-purple">试用</span>
                                    <?php endif; ?>
                                    <?php if ($od->goodsDetail->free_post == 1): ?>
                                        <span class="icon icon-purple">免邮</span>
                                    <?php endif; ?>

                                </div>
                                <?= $order->getFirstError($od->product_id); ?>

                            </div>
                            <div class="shop-size">
                                <?php
                                if (empty($od->productDetail->spec_desc)) {
                                    ?>
                                    <span class="size-name">&nbsp;</span>
                                    <?php
                                } else {
                                    foreach (explode(',', $od->productDetail->spec_desc) as $item):
                                        if (empty($item)) continue;
                                        list($k, $v) = explode(':', $item);
                                        ?>
                                        <span class="size-name"><?= $k ?></span><span>:</span><span
                                        class="size-value"><?= $v ?></span>
                                    <?php endforeach;
                                }
                                ?>

                            </div>


                            <div class="shop-price">
                                <?php if ($od->product->price_type == Product::$_PRICE_TYPE_RMB): ?>
                                    <span
                                        class="price-rmb">￥<?= Yii::$app->formatter->asDecimal($od->product->sale_price, 2) ?></span>
                                <?php else: ?>
                                    <span class="price-sd"><?= $od->product->try_price ?>试豆</span>
                                <?php endif; ?>
                            </div>

                            <div class="shop-amount"><?= $od->number ?></div>


                            <div class="shop-sum">
                                <?php if ($od->product->price_type == Product::$_PRICE_TYPE_RMB): ?>
                                    <span
                                        class="sum-rmb">￥<?= Yii::$app->formatter->asDecimal($od->number * $od->product->sale_price, 2) ?></span>
                                <?php else: ?>
                                    <span class="sum-sd"><?= $od->number * $od->product->try_price ?> 试豆</span>
                                <?php endif; ?>
                            </div>
                        </li>
                    <?php endforeach; ?>
                </ul>
            </div>
        <?php endif; ?>
    </div>
    <!--邮费金额-->
    <div class="settle-post">
        <h2 class="settle-sub-title">邮费金额</h2>
        <div class="settle-post-info">您的邮费是：<strong>￥<i id="expressCharge"><?= $order->ship_amount_discount ?></i></strong>
            <?php
            #var_dump($order);
            if (($order->try_coin < $order->score && $order->ship_amount_discount > 0) ||
                ($order->ship_amount_discount == 0 && !$order->member_address_id) ):
                ?>
                <span class="settle-post-toggle" id="settlePostToggle"><span id="isUseCoin"
                                                                             class="icon icon-stock"></span>
            使用<i id="settleSdAmountDeduct"><?= $order->ship_amount * $order->money_convery_to_money ?></i>试豆抵扣</span>
                <span class="error"><?= $order->getFirstError('ship_amount_try_coin'); ?></span>
            <?php endif; ?>
        </div>
    </div>
    <!--邮费金额-->
    <div class="settle-total">
        <div class="settle-total-in">
            <div class="settle-total-info">
                <ul>
                    <li>
                        <div class="settle-total-name"><i><?= count($order->productsInCart) ?></i>件商品总价：</div>
                        <div class="settle-total-value">￥<?= $order->goods_amount ?>+<?= $order->try_coin ?>试豆</div>
                    </li>
                    <li>
                        <div class="settle-total-name">邮费金额：</div>
                        <div class="settle-total-value">¥<i
                                id="settleExpressCharge"><?= $order->ship_amount_discount ?></i></div>
                    </li>
                    <li>
                        <div class="settle-total-name">优惠金额：</div>
                        <div class="settle-total-value">-¥<i id="reduceAmount"><?= $order->reduce_amount ?></i></div>
                    </li>
                </ul>
            </div>
            <div class="settle-total-result">
                <p>应付金额：<strong class="size2">￥<i id="settleAmount"><?= $order->order_amount ?></i></strong>+<strong
                        id="settleSdAmount"><?= $order->try_coin ?></strong>试豆</p>
                <div class="settle-total-go"><a
                        href="<?= Yii::$app->urlManager->createUrl(["cart/index"]); ?>">返回购物车</a>
                    <div class="settle-total-submit" id="settleSubmit">提交订单</div>
                </div>
            </div>
        </div>
    </div>
</div>


<?php $this->beginBlock('js'); ?>
<script src="<?= Yii::getAlias('@static') ?>/pc/common/js/jquery-1.12.3.min.js"></script>
<script src="<?= Yii::getAlias('@static') ?>/pc/common/js/lkt.pc.js"></script>
<script src="<?= Yii::getAlias('@static') ?>/pc/liketry/js/order.js"></script>
<script>
    <?php
    if(key_exists('fatal', $errs)):
    ?>
    $.alert('<?=$errs['fatal'][0]?>', function () {
        window.location.href = '<?=Yii::$app->urlManager->createUrl('cart')?>';
    });
    <?php endif;?>
    var areaUrl = '<?=Yii::$app->urlManager->createUrl(["member-address/select-data", 'parent_id' => '']);?>';
    var addAddressUrl = '<?=Yii::$app->urlManager->createUrl(["member-address/add"]);?>';
    var delAddressUrl = '<?=Yii::$app->urlManager->createUrl(["member-address/del"]);?>';
    var editAddressUrl = '<?=Yii::$app->urlManager->createUrl(["member-address/edit"]);?>';
    var getAddressList = '<?=Yii::$app->urlManager->createUrl(["member-address/consult"]);?>';
    var getAddressUnit = '<?=Yii::$app->urlManager->createUrl(["member-address/address-info", 'uid' => '']);?>';
    var getCellCodeUrl = '<?=Yii::$app->urlManager->createUrl(["member-info/cell-get-code"]);?>';
    var checkCellCodeUrl = '<?=Yii::$app->urlManager->createUrl(["member-info/cell-confirm"]);?>';
    var getCellCodeSettleBalanceUrl = '<?=Yii::$app->urlManager->createUrl(["order/member-money-sms"]);?>';
    var checkCellCodeSettleBalanceUrl = '<?=Yii::$app->urlManager->createUrl(["order/member-money-sms-check"]);?>';
    var checkSdAmount = '<?=Yii::$app->urlManager->createUrl(["order/validate"]);?>';
    var settleOrderUrl = '<?=Yii::$app->urlManager->createUrl(["order/save"]);?>';
    var settleSuccessUrl = '<?=Yii::$app->urlManager->createUrl(["order/success", 'order_info_id' => '']);?>';
    var orderData = {
        cart_ids: '<?=implode(',', $order->cart_ids)?>',
        member_address_id: 0,
        use_coin_for_ship_amount: 0,
        member_money: 0,
        order_amount: '<?=$order->order_amount?>',
        try_coin: '<?= $order->try_coin ?>',
		ship_amount_discount: '<?= $order->ship_amount_discount ?>'
    };
    var settleSuccessUrlPayed = '<?=Yii::$app->urlManager->createUrl(["order/payed", 'order_info_id' => '']);?>';
</script>
<?php $this->endBlock(); ?>

<textarea id="addressEditHtml" class="hidden">
<div class="settle-address-edit-form">
    <form id="settleAddressForm">
        <div class="settle-address-edit-unit edit-receiver"><label for="editReceiver">* 收货人：</label><input
                name="consignee" type="text" maxlength="10" id="editReceiver" class="width156" required=true><span
                class="error"></span></div>
        <div class="settle-address-edit-unit edit-area"><label for="editProvince">* 所在地区：</label>
            <select name="s1_uid" id="editProvince" class="width124" layer="1" required=true>
                <option value="">请选择</option>
            </select><select name="s2_uid" id="editCity" class="width124" layer="2" required=true>
                <option value="">请选择</option>
            </select><select name="s3_uid" id="editCounty" class="width124" layer="3" required=true>
                <option value="">请选择</option>
            </select><span class="error"></span></div>
        <div class="settle-address-edit-unit edit-area-detail"><label for="editAreaDetail">* 详细地址：</label><input
                type="text" name="address" id="editAreaDetail" class="width408" required=true><span
                class="error"></span></div>
        <div class="settle-address-edit-unit tel"><label for="tel">* 手机号码：</label><input maxlength="11" type="text"
                                                                                         name="cell"
                                                                                         id="tel" class="width156"
                                                                                         required=true><span
                class="error"></span></div>
        <div class="settle-address-edit-unit tel"><label for="tel1">固定电话：</label><input type="text" name="tel1"
                                                                                        id="tel1"
                                                                                        class="width52">-<input
                type="text" name="tel2" id="tel2" class="width156"><span class="error"></span></div>
        <div class="settle-address-edit-unit zipcode"><label for="postcode">邮编：</label><input type="text" name="zipcode"
                                                                                              id="postcode"
                                                                                              class="width156"><span
                class="error"></span></div>
        <div class="settle-edit-form-set-default"><span class="icon icon-stock"></span><span>设为默认地址</span></div>
    </form>
</div>
</textarea>

